package jlu.com.p2pclient.utils;

import android.app.Application;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @Author freedom
 * @DATE 2017/5/17 0017 19:35.
 * see   手机参数获取类
 */

public class Config extends Application {

    private static final String TAG = "Config";
    public static final String DEVICE_ID = Build.SERIAL;//手机序列号
    public static final String FILESEPARATOR = System.getProperty("file.separator");//路径分隔符
    public static final String SERVER_IP = "49.140.162.186";
    public static final int SERVER_PORT = 8089;
    public static final String TAG_TOTAL_MEMORY = "totalMemory";
    public static final String TAG_AVAIL_MEMORY = "availMemory";
    public static final String TAG_THRESHOLD = "threshold";
    public static final String TAG_IS_LOWMEMORY = "isLowMemory";
    public static final String TAG_LEFT_POWER = "leftPower";
    public static final String TAG_CPU_FREQUENCY = "cpuFrequency";
    public static final String SERVER_REPLY = "OK";
    public static final String NODE_IP = "ip";

    /* Checks if external storage is available for read and write */
    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public static boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

    public static String getFilePathFromUri(Context context, Uri uri) {

        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = {"_data"};
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

    /**
     * see   获取网关地址
     *
     * @入参 void
     * @出参 String ip地址
     */
    public static String getHostIp() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
                        .hasMoreElements(); ) {
                    InetAddress inetAddress = ipAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
        } catch (Exception e) {
        }
        return null;
    }
}
